package com.admission.web;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.PropertyFilter;
import org.springside.modules.utils.web.struts2.Struts2Utils;

import com.admission.model.AdmissionPolicy;
import com.admission.service.AdmissionPolicyManager;
import com.admission.util.FileUtils;
import com.admission.util.PageUtils;
import com.opensymphony.xwork2.ActionContext;

@Namespace("/base")
@Results({@Result(name = CrudActionSupport.RELOAD, location = "/base/index.action", type = "redirect") })
public class AdmissionPolicyAction extends CrudActionSupport<AdmissionPolicy> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 8205916110066860366L;

	private AdmissionPolicy entity;
	private AdmissionPolicyManager admissionPolicyManager;
	private Page<AdmissionPolicy> page = new Page<AdmissionPolicy>(
			PageUtils.DEFAULT_PAGE_SIZE);
	// 搜索的关键字，默认为空
	private String keywords = "";
	private String id;

	public AdmissionPolicy getModel() {
		return entity;
	}

	// 异步查询访问人数
	public String visitorCount() throws IOException {
		String path = ServletActionContext.getServletContext().getRealPath(
				"/WEB-INF");
		String r = "";
		path += "/other/visitCount.properties";
		File f = new File(path);
		if (f.exists()) {
			f.createNewFile();
		}
		ActionContext actionContext = ActionContext.getContext();
		Map session = actionContext.getSession();
		if (session.get(id) == null) {
			r = FileUtils.getProperties(path, id, "1", true);
			session.put(id, "1");
		} else {
			r = FileUtils.getProperties(path, id, "1", false);
		}

		Struts2Utils.renderText(r);
		return null;
	}

	@Override
	public String list() throws Exception {
		List<PropertyFilter> filters = PropertyFilter
				.buildFromHttpRequest(Struts2Utils.getRequest());

		// 如果有关键字输入，则添加模糊搜索条件到filters
		if (keywords != "") {
			PropertyFilter p = new PropertyFilter("LIKES_title", keywords);
			filters.add(p);
		}

		// 设置默认排序方式
		if (!page.isOrderBySetted()) {
			page.setOrderBy("inputTime");
			page.setOrder(Page.DESC);
		}
		page = admissionPolicyManager.searchAdmissionPolicy(page, filters);
		return SUCCESS;
	}

	@Override
	public String input() throws Exception {
		return INPUT;
	}

	@Override
	public String save() throws Exception {
		admissionPolicyManager.saveAdmissionPolicy(entity);
		return this.list();
	}

	@Override
	public String delete() throws Exception {
		// TODO Auto-generated method stub
		return RELOAD;
	}

	@Override
	protected void prepareModel() throws Exception {
		if (StringUtils.isEmpty(id)) {
			entity = new AdmissionPolicy();
		} else {
			entity = admissionPolicyManager.getById(id);
		}
	}

	public AdmissionPolicy getEntity() {
		return entity;
	}

	public void setEntity(AdmissionPolicy entity) {
		this.entity = entity;
	}

	public Page<AdmissionPolicy> getPage() {
		return page;
	}

	public void setPage(Page<AdmissionPolicy> page) {
		this.page = page;
	}

	public String getKeywords() {
		return keywords;
	}

	public void setKeywords(String keywords) {
		this.keywords = keywords;
	}

	@Autowired
	public void setAdmissionPolicyManager(
			AdmissionPolicyManager admissionPolicyManager) {
		this.admissionPolicyManager = admissionPolicyManager;
	}

	public void setId(String id) {
		this.id = id;
	}

}
